package com.blb.community_admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.blb.community_admin.Annotation.Log;
import com.blb.community_admin.entity.SysOperLog;
import com.blb.community_admin.service.IOperationLogService;
import com.blb.community_admin.util.EasyExcelUtil;
import com.blb.community_admin.util.ResponseResult;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author hkp
 */
@RestController
public class OperationLogController {

    @Resource
    private IOperationLogService operationLogService;

    /**
     * 分页模糊查询
     *
     * @param current
     * @param size
     * @param title
     * @param operName
     * @param businessType
     * @param status
     * @param operTime
     * @return
     */
    @GetMapping("/operationLog/page")
    public ResponseResult<IPage<SysOperLog>> selectOperationLogPage(Integer current, Integer size, String title, String operName, Integer businessType, Integer status, String operTime, String prop, String order) {
        /**
         * @param startTime 开始时间
         * @param endTime 结束时间
         */
        String startTime = null;
        String endTime = null;
        if (StringUtils.isNotEmpty(operTime)) {
            String[] split = operTime.split(",");
            startTime = split[0];
            endTime = split[1];
        }
        if ("ascending".equals(order)) {
            order = "ASC";
        } else if ("descending".equals(order)) {
            order = "DESC";
        }
        if (StringUtils.isNotEmpty(prop)) {
            if ("operName".equals(prop)) {
                prop = "oper_name";
            } else {
                prop = "oper_time";
            }
        }
        IPage<SysOperLog> page = operationLogService.selectOperationLogPage(current, size, title, operName, businessType, status, startTime, endTime, prop, order);
        return ResponseResult.ok(page);
    }

    /**
     * 清空所有操作日志
     *
     * @return
     */
    @Log(module = "操作日志", operationType = 9, requestMethod = "DELETE")
    @DeleteMapping("/operationLog/all")
    public ResponseResult<String> deleteAll() {
//        查询所有数据
        List<SysOperLog> list = operationLogService.list(null);
//        映射成operId的集合
        List<Long> operIdList = list.stream().map(SysOperLog::getOperId).collect(Collectors.toList());
        operationLogService.removeByIds(operIdList);
        return ResponseResult.ok("ok");
    }

    /**
     * 批量删除
     *
     * @param selections
     * @return
     */
    @DeleteMapping("/operationLog/delete")
    public ResponseResult<String> deleteByIds(@RequestBody List<SysOperLog> selections) {
        List<Long> operIdList = selections.stream().map(SysOperLog::getOperId).collect(Collectors.toList());
        operationLogService.removeByIds(operIdList);
        return ResponseResult.ok("ok");
    }

    /**
     * 导出
     *
     * @param operLogList
     * @return
     * @throws IOException
     */
    @PostMapping("/operationLog/export")
    public ResponseEntity<ByteArrayResource> export(@RequestBody List<SysOperLog> operLogList) throws IOException {
        String path = EasyExcelUtil.write("操作日志", SysOperLog.class, operLogList);
        ResponseEntity<ByteArrayResource> download = EasyExcelUtil.download(path);
        return download;
    }

    /**
     * 查询所有operationList
     *
     * @return
     */
    @GetMapping("/operationLog")
    public ResponseResult<List<SysOperLog>> selectAll() {
        List<SysOperLog> list = operationLogService.list(null).stream().distinct().collect(Collectors.toList());
        ;
        return ResponseResult.ok(list);
    }
}
